package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkServiceImpl implements WorkService {
    @Autowired
    WorkMapper workMapper;

    @Autowired
    UserMapper  userMapper;

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO businessData(LocalDate Begin, LocalDate End) {
        Map map=new HashMap();
        map.put("begintime",  LocalDateTime.of(Begin, LocalTime.MIN));
        map.put("endtime", LocalDateTime.of(End, LocalTime.MAX));

//        完成率=完成count status=5  / 总 status=null
//        平均客单价 完成sum status=5  / 总 status=null
        //开始算已经完成的订单
        map.put("status",5);
        Double turnover = orderMapper.sumByTimeStatus(map);
        if (turnover==null){
            turnover=0.0;
        }
        Integer completedCount = orderMapper.countByTimeStatus(map);
        if (completedCount==null){
            completedCount=0;
        }

        Integer totalOrder = orderMapper.countByTimeStatus(map);
        Double unitPrice = 0.0;
        Double orderCompletionRate=0.0;
        if(totalOrder==null || totalOrder==0){
            totalOrder=0;
        }else{
            orderCompletionRate= (double) (completedCount/totalOrder);
            unitPrice=turnover/totalOrder;
        }

        return BusinessDataVO.builder()
                .newUsers(userMapper.newUserList(map))
                .turnover(turnover)
                .unitPrice(unitPrice)
                .orderCompletionRate(orderCompletionRate)
                .validOrderCount(completedCount)
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        Map map=new HashMap();
        map.put("begintime",  LocalDateTime.of(LocalDate.now(), LocalTime.MIN));
        map.put("endtime", LocalDateTime.of(LocalDate.now(), LocalTime.MAX));
        Integer allOrders = orderMapper.countByTimeStatus(map);
        map.put("status",Orders.CANCELLED);
        Integer CANCELLED = orderMapper.countByTimeStatus(map);

        map.put("status",Orders.COMPLETED);
        Integer COMPLETED = orderMapper.countByTimeStatus(map);

        map.put("status",Orders.DELIVERY_IN_PROGRESS);
        Integer DELIVERY_IN_PROGRESS = orderMapper.countByTimeStatus(map);

        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer TO_BE_CONFIRMED = orderMapper.countByTimeStatus(map);


        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(CANCELLED)
                .waitingOrders(TO_BE_CONFIRMED)
                .completedOrders(COMPLETED)
                .deliveredOrders(DELIVERY_IN_PROGRESS)
                .build();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        return DishOverViewVO.builder()
                .sold(dishMapper.countByStatus(1))
                .discontinued(dishMapper.countByStatus(0))
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeal() {

        return SetmealOverViewVO.builder()
                .discontinued(setmealMapper.countByStatus(0))
                .sold(setmealMapper.countByStatus(1))
                .build();
    }
}
